﻿using Message;
using UnityEngine;
using UnityEngine.UI;

public class RolePanel : PanelBase
{
    private Transform creatPanel;
    private InputField nameField;
    private Button creatBtn;
    private Button backBtn;  
    private Button creatRoleBtn;
    private Dropdown dropdown;

    private Button chooseRoleBtn;
    private Button startRoleBtn;

    private Button enterBtn;
     
    public override void Init(params object[] args)
    {
        base.Init(args);
        layer = PanelLayer.Panel;
    }

    public override void OnShowing()
    {
        base.OnShowing();
        Transform skinTrans = panelObj.transform;
        creatPanel = skinTrans.Find("CreatPanel");
        nameField = creatPanel.Find("name").GetComponent<InputField>();
        dropdown = creatPanel.Find("Dropdown").GetComponent<Dropdown>();
        creatBtn = creatPanel.Find("CreatBtn").GetComponent<Button>();
        creatBtn.onClick.AddListener(OnCreatRole);
        backBtn = creatPanel.Find("BackBtn").GetComponent<Button>();
        backBtn.onClick.AddListener(OnExitCreatRole);   
        creatPanel.gameObject.SetActive(false);

        chooseRoleBtn = skinTrans.Find("chooseRoleBtn").GetComponent<Button>();
        chooseRoleBtn.onClick.AddListener(OnChoseRole);
        startRoleBtn = skinTrans.Find("startRoleBtn").GetComponent<Button>();
        startRoleBtn.onClick.AddListener(OnStartRole);

        enterBtn = skinTrans.Find("enterBtn").GetComponent<Button>();
        enterBtn.onClick.AddListener(OnEnter);
    }
    
    /// <summary>
    /// 创建角色
    /// </summary>
    private void OnCreatRole()
    {
        if(nameField.text=="")
        {
            PanelMgr.Instance.OpenPanel<TipPanel>("TipPanel", "角色名不能为空!").Close(1.5f);
            return;
        }
        //注册创建角色返回结果回调
        MsgMgr.Instance.AddListener(typeof(SToCCreatRoleRet).ToString(), OnCreatRoleBack);
        //发送
        CToSCreatRole protocol = new CToSCreatRole();
        protocol.uid = GlobeData.uid;
        protocol.name = nameField.text;
        protocol.type = dropdown.value;
        NetMgr.Instance.Send(protocol);
    }

    private void OnCreatRoleBack(object[] args)
    {
        Loom.QueueOnMainThread(() =>
        {
            //移除登录返回结果回调
            MsgMgr.Instance.RemoveListener(typeof(SToCCreatRoleRet).ToString(), OnCreatRoleBack);

            SToCCreatRoleRet stcl = args[0] as SToCCreatRoleRet;
            if (stcl.code == 0)
            { 
                PanelMgr.Instance.OpenPanel<TipPanel>("TipPanel", "创建成功!").Close(1.5f); 
            }
            else
            {
                PanelMgr.Instance.OpenPanel<TipPanel>("TipPanel", "创建失败!").Close(1.5f); 
            }
        });
    }

    private void OnExitCreatRole()
    {
        creatPanel.gameObject.SetActive(false);
    }

    private void OnChoseRole()
    {
        //打开选择角色面板
    }

    private void OnStartRole()
    {
        creatPanel.gameObject.SetActive(true);
    }

    private void OnEnter()
    {
        PanelMgr.Instance.OpenPanel<MainPanel>("MainPanel");
        Close();
    }  

}
